﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class shownews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptdatabind();
            rptcommentbind();
        }
    }

    private void rptdatabind()
    {
        if (Request.QueryString["id"] != null && Request.QueryString["op"] =="news")
        {
            YqhData obj = new YqhData();
            string sql = string.Format("select nid,naddname,tname,naddname,nsubject,ncontent,ndate from news inner join newstype on news.ntypeid = newstype.tid where nid={0} order by ndate desc", Request.QueryString["id"].ToString());
            rptnews.DataSource = obj.FillDataSet(sql);
            rptnews.DataBind();
        }
    }
    private void rptcommentbind()
    {
        if (Request.QueryString["id"] != null && Request.QueryString["op"] == "news")
        {
            YqhData obj = new YqhData();
            string hot = string.Format(@"update news set nhot=nhot+1
where nid={0}", Request.QueryString["id"]);
            obj.ExecuteSql(hot);
            string sql = string.Format(@"select uheadurl,cname,cip,ccontent,cdate=CONVERT(VARCHAR(23),cdate,111) 
from news inner join comment on cnewsid = nid inner join userinfo 
on uname = cname
where nid={0}",Request.QueryString["id"].ToString());
            rptcomment.DataSource = obj.FillDataSet(sql);
            rptcomment.DataBind();
        }
    }
    protected void btnup_Click(object sender, EventArgs e)
    {
        YqhData obj = new YqhData();
        if (Session["userlogin"] != null)
        {
            if (txtcontent.Text != "")
            {
                if (txtyzm.Text.ToLower() == Session["yzm"].ToString().ToLower())
                {
                    string sql = string.Format(@"insert into comment
                    (cname,
                    cnewsid,
                    ccontent,
                    cip,
                    cdate)
                        values
                            ('{0}',
                              {1},
                             '{2}',
                             '{3}',
                             getdate()
                            )",
                                      Session["userlogin"].ToString(),
                                      Request.QueryString["id"],
                                      txtcontent.Text,
                                      Page.Request.UserHostAddress
                                      );
                    obj.ExecuteSql(sql);
                    rptcommentbind();
                }
                else
                {
                    obj.ShowMsg("验证码错误");
                }
            }
            else if (txtcontent.Text == "")
            {
                obj.ShowMsg("评论内容不能为空");
            }
        }
        else if (Session["userlogin"] == null)
        {
            Session["newsurl"] = Request.Url.ToString(); 
            obj.ShowMsg("请先登录", "index.aspx");
        }
    }
}
